<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 * $Id: 2017-12-13 03:39:35 apple $
*/
namespace core\member;
defined('BY_JYA') or exit('error');
//提现

class cash{
	function __construct(){
		$setting=uni_setting('cash');
		$this->setting=$setting;
	}
	function check_open(){
		return $this->setting['open']?true:false;
	}
	/*
		检查提现金额是否符合要求！
	*/
	function check_cash_fee($mid,$fee){
		if(!$this->check_open()){
			exi('系统不支持提现！','error');
		}
		if(!$fee){
			exi('提现失败，未设置提现金额！','error');
		}
		$member=model('core/member')->detail($mid,array('credit2'));
		if(!$member){
			exi('提现失败，指定会员不存在！','error');
		}
		if($this->setting['min'] && $fee<$this->setting['min']){
			exi("提现失败，提现金额需大于{$this->setting['min']}元！",'error');
		}
		if($member['credit2']<$fee){
			exi('提现失败，会员账户余额不足！','error');
		}
		model('core/member')->cut_credit2($mid,$fee,'会员提现支出','core');
		model('core/member')->fz_credit2($mid,$fee,'会员提现冻结','core');
		return true;
	}
	/*
		执行提现
	*/
	function pay($cash_id){
		$cash=pdo_get('core_member_cash',array('id'=>$cash_id));
		if(!$cash){
			exi('提现打款失败，提现订单不存在！','error');
		}
		if($cash['status']){
			exi('提现打款失败，该提现订单已打款！','error');
		}
		if(!$cash){
			exi('提现打款失败，提现订单不存在！','error');
		}
		$this->check_cash_fee($cash['mid'],$cash['money']);
		$params=array(
			'money'=>$cash['cash_money'],
			'remark'=>'会员余额提现',
		);
		$pay_out_id=model('core/member')->pay_out($cash['mid'],$params);
		$update=array(
			'status'=>1,
			'pay_out_id'=>$pay_out_id,
		);
		pdo_update('core_member_cash',$update,array('id'=>$cash['id']));
		model('core/member')->thaw_credit2($cash['mid'],$cash['money'],'会员提现成功','core');
		return true;
	}
}